﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel.DomainServices.Client;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AppLab.Web;
using AppLab.Web.Services;
using Wicresoft.AppFlow.Flows;


namespace AppLab.Views.MobileBasicInfo
{

    public partial class NewMobileBrandWindow : ChildWindow
    {
        private IList<OperationBase> possiblyPendingOperations = new List<OperationBase>();
        FlowContext flow = new FlowContext();
        private Brand mbBrand = new Brand();
        private ComboBox cbox = new ComboBox();

        public NewMobileBrandWindow()
        {
            InitializeComponent();

            var query = flow.GetBrandsQuery();
            var loadOp = flow.Load(query);

            cbox.ItemsSource = loadOp.Entities;
            cbox.DisplayMemberPath = "BrandName";
        }

        private Brand mbBrN = new Brand();

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(this.tbMbBrand.Text))
            {
                MessageBox.Show("请输入新的手机品牌！");
                return;
            }
            mbBrN.BrandName = this.tbMbBrand.Text;

            if (IsAddNewBrand())
            {
                MessageBox.Show("该手机品牌已经存在！");
                return;
            }

            this.flow.AddMobileBrand(mbBrN);
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        /// <summary>
        ///  返回TRUE，表示已经存在该品牌
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        private bool IsAddNewBrand()
        {
            bool isAddNewBrand = false;
            for (int i = 0; i < cbox.Items.Count;i++ )
            {

                if (this.tbMbBrand.Text.Equals((cbox.Items[i] as Brand).BrandName))
                {
                    isAddNewBrand = true;
                    break;
                }
            }
            //var query = flow.GetBrandsQuery();
            //var loadOp = flow.Load(query);
            //List<Brand> brandList = loadOp.Entities.ToList();
            //foreach (Brand brand in brandList)
            //{
            //    if (e.BrandName.Equals(brand.BrandName))
            //    {
            //        isAddNewBrand = true;
            //        break;
            //    }
            //}
            return isAddNewBrand;
        }

        ////调用的异步方法：this.flow.SearchSameBrand(mbBrN)
        ////声明委托
        //private delegate bool SearchBrandDelegate(Brand e, out bool result);
        
        //public void GetCompetenceOfAddNewBrand()
        //{
        //    bool result;
        //    //定义一个委托,并实例化
        //    SearchBrandDelegate sarBadgte = new SearchBrandDelegate(this.flow.SearchSameBrand);
        //    IAsyncResult iAR = sarBadgte.BeginInvoke(mbBrN, out result, null, null);
        //    //阻塞到发出 WaitHandle 信号，然后调用 EndInvoke
        //    iAR.AsyncWaitHandle.WaitOne();
        //    //调用 EndInvoke
        //    sarBadgte.EndInvoke(out result, iAR);
        //}
    }
}

